package com.xinlianshiye.im

import com.fasterxml.jackson.core.JsonProcessingException
import com.fasterxml.jackson.databind.ObjectMapper
import com.xinlianshiye.im.bean.Record
import com.xinlianshiye.im.response.RecordView2.Companion.init
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class ResponseBody private constructor() {
    /**
     * 消息类型 0: 请求, 1: 响应, 2: 通知, 3: 应答
     */
    var msgtype: MessageType? = null

    /**
     * 命令类型 0: 绑定, 1: 心跳, 2: 上线, 3: 下线, 4: 聊天, 5: 错误, 6: http 请求替换
     * 对应枚举: [CmdType]
     */
    var cmd: CmdType? = null

    /**
     * 发送人
     */
    var sender: Int? = null

    /**
     * 接收人
     */
    var receiver: Int? = null

    /**
     * 请求数据
     */
    var content: String? = null

    /**
     * 请求应用端
     */
    var client: String? = null
    var url: String? = null
    var errorcode: Int? = null
    var reason: String? = null

    companion object {
        private val log:Logger = LoggerFactory.getLogger(ResponseBody::class.java)

        fun init(requestBody: RequestBody): ResponseBody {
            val responseBody = ResponseBody()
            responseBody.msgtype = requestBody.msgtype
            responseBody.cmd = requestBody.cmd
            responseBody.url = requestBody.url
            responseBody.client = requestBody.client
            return responseBody
        }

        @JvmStatic
        fun initError(code: Int?, reason: String?): ResponseBody {
            val responseBody = ResponseBody()
            responseBody.msgtype = MessageType.response
            responseBody.cmd = CmdType.offerror
            responseBody.errorcode = code
            responseBody.reason = reason
            return responseBody
        }

        @JvmStatic
        fun initSuccess(url: String?, result: Result?): ResponseBody {
            val response = ResponseBody()
            response.msgtype = MessageType.response
            response.cmd = CmdType.httprequest
            response.url = url
            try {
                if (result != null) {
                    response.content = ObjectMapper().writeValueAsString(result)
                }
            } catch (e: JsonProcessingException) {
            }
            return response
        }

        @JvmStatic
        fun initChat(sender: Int?, receiver: Int?, record: Record?): ResponseBody {
            val responseBody = ResponseBody()
            responseBody.msgtype = MessageType.response
            responseBody.cmd = CmdType.chat
            responseBody.sender = sender
            responseBody.receiver = receiver
            try {
                responseBody.content = ObjectMapper().writeValueAsString(init(record!!))
            } catch (e: JsonProcessingException) {
                log.error("Record 序列化错误!", e)
            }
            return responseBody
        }

        @JvmStatic
        fun initPing(receiver: Int?): ResponseBody {
            val responseBody = ResponseBody()
            responseBody.msgtype = MessageType.response
            responseBody.cmd = CmdType.ping
            responseBody.receiver = receiver
            responseBody.url = "pingPongMap"
            responseBody.content = "pong"
            return responseBody
        }
    }
}